package cn.hesion.charset;

import java.io.*;
import java.nio.Buffer;

public class FileRW {
    public static void main(String[] args) throws Exception {
        RWFile();
    }

    public static void RWFile() throws Exception {
        String file = "1.txt";
        String charset = "UTF-8";
        //写字符转换成字节流
        FileOutputStream outputStream = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, charset);
        try {
            writer.write("这是保存的中文字符2");
        } finally {
            writer.close();
        }

        //读取字节转换成字符
        FileInputStream inputStream = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(inputStream, charset);
        StringBuffer buffer = new StringBuffer();
        char[] buf = new char[64];
        int count=0;
        try {
            while ((count=reader.read(buf)) != -1){
                buffer.append(buf,0,count);
            }
        }finally {
            reader.close();
        }
        System.out.println("buffer: "+buffer);
    }
}
